package mybatis.util;

import mybatis.entity.Supplyer;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;


public class CSVTranform {
	public static Set<String> tranformCSV() {
		Set<String> list = new HashSet<>();
		try (BufferedReader br = new BufferedReader(new InputStreamReader(CSVTranform.class.getClassLoader().getResourceAsStream("gameType.csv")))) {
		    // CSV文件的分隔符
		    String DELIMITER = ",";
		    // 按行读取
		    String line;
		    line = br.readLine();
		    while ((line = br.readLine()) != null) {
		        // 分割
		    	String line1 = line.replaceAll("\\[", "").replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\'", "").replaceAll("\"", "").replace(" ", "").replace("-", "");
		        String[] columns = line1.split(DELIMITER);
		        for(String s : columns) {
		        	if(s.equals("-") || s.isEmpty()) {
						continue;
					}
		        	list.add(s);
		        }
		    }
		} catch (IOException ex) {
		    ex.printStackTrace();
		}
		System.out.println(list.size());
		String s = "–";
		list.remove("–");
		for(String s1 : list) {
			if(s1.equals("–")) System.out.println("找到了");
		}
		System.out.println(list);;	    	
		return list;
	}
	public static Set<Supplyer> tranformCSVtoObject() {
		Set<Supplyer> list = new HashSet<>();
		
		try (BufferedReader br = new BufferedReader(new InputStreamReader(CSVTranform.class.getClassLoader().getResourceAsStream("gamesupply.csv")))) {
		    // CSV文件的分隔符
		    // 按行读取
		    String line;
		    line = br.readLine();
		    int count = 1;
		    while ((line = br.readLine()) != null) {
		    	count++;
		    	String line2 = line.replace("\"{", "{").replaceAll("}\"", "}").replaceAll("\"\"", "\"");
		        // 分割
		    	ObjectMapper om = new ObjectMapper();
		    	Supplyer user = om.readValue(line2, Supplyer.class);
		    	System.out.println(count+":"+user);

		    }
		} catch (IOException ex) {
		    ex.printStackTrace();
		}
//		System.out.println(list.size());
//		String s = "–";
//		list.remove("–");
//		for(String s1 : list) {
//			if(s1.equals("–")) System.out.println("找到了");
//		}
//		System.out.println(list);;	    	
		return list;
	}
	public static void main(String[] args) {
		tranformCSVtoObject();
//		Set<String> set = tranformCSV();
//		TypeService service = new TypeServiceImpl();
//		for(String s:set) {
//			service.add(s);
//		}
	}
}
